package com.cnc.bas.util;

import com.cnc.bas.ConfigManager;

import java.util.List;

/**
 * Created by Longtop
 * User: Dubf
 * Date: 2004-10-19
 * Time: 9:22:17
 */
public final class PageControl {
    private int intPageSize = 20 ;
    private static final int intMaxResults = Integer.parseInt(ConfigManager.getInstance().getConfigValue("MaxResults", "3000"));

    public  int getTotalPages(List list) {
        if (list == null) return 0;
        int intTotalRecords = list.size();
        int intTotalPages = (intTotalRecords + intPageSize - 1) / intPageSize;
        return intTotalPages;
    }

    public  List getPageList(int intCurrPage, List list) {
        if (list == null) return null;
        intCurrPage = intCurrPage <= 1 ? 1 : intCurrPage;
        int intTotalRecords = list.size();
        int intOffset = (intTotalRecords < intPageSize * intCurrPage) ? intTotalRecords - intPageSize * (intCurrPage - 1) : intPageSize;
        int intFromIndex = (int) (intPageSize * (intCurrPage - 1));
        return list.subList(intFromIndex, intFromIndex + intOffset);
    }

    public  int getFromIndex(int intCurrPage, List list) {
        if (list == null) return 0;
        intCurrPage = intCurrPage <= 1 ? 1 : intCurrPage;
        return (int) (intPageSize * (intCurrPage - 1));
    }

    public  int getToIndex(int intCurrPage, List list) {
        if (list == null) return 0;
        intCurrPage = intCurrPage <= 1 ? 1 : intCurrPage;
        int intTotalRecords = list.size();
        int intOffset = (intTotalRecords < intPageSize * intCurrPage) ? intTotalRecords - intPageSize * (intCurrPage - 1) : intPageSize;
        int intFromIndex = (int) (intPageSize * (intCurrPage - 1));
        return intFromIndex + intOffset;
    }

    public  List getPageList(int intFromIndex, int intToIndex, List list) {
        return list.subList(intFromIndex, intToIndex);
    }

    public  int getMaxResult(List list) {
        return (list.size() >= intMaxResults ? intMaxResults : 0);
    }

	public  void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}
}